<?php
	
require '../include/user_login.inc.php';
require '../include/paging.inc.php';

if($_SESSION['ACCESS_SID'] < 1) redirect(HTML_BASE.$_SESSION["lang"].'/login/');

$redirect = $_SERVER['HTTP_REFERER'];	
$user_id = $_SESSION['USER_SID'];
	

if($_SESSION['ACCESS_SID'] == 1)
{
		$sql = "SELECT
				n.note_id,
				n.title,
				n.description,
				n.priority,
				n.status,
				n.by_user,
				n.start_date,
				n.end_date
			FROM
				notes AS n
			WHERE
				
				n.by_user = '$user_id'
				
		";
}

if($_SESSION['ACCESS_SID'] == 2)
{
	
		if( $_GET['delete'])
		{
			
			
			$sql = "DELETE FROM 
               notes	
            WHERE
               note_id = '". $_GET['delete']."'
             ";
  			query($sql);
			redirect(HTML_BASE.$_SESSION['lang'].'/tasks');
		}
	
		if($_POST["is_note_sub"])
		{
			$error_message = '';
				
				if (!$_POST['title']) $error_message .= 'Не е въведенa Тема<br/>';
				
				
				if (!$error_message) {
					$sql = "INSERT INTO notes
								 (title,priority,status,by_user,description,end_date)
							   VALUES
								 ('".escape($_POST['title'])."','".$_POST['priority']."','1','".$_SESSION['client']."','".$_POST['description']."','".$_POST['creation_datetime']."')
							   
							";
		
						query($sql);
						$id = mysql_insert_id();
						redirect(HTML_BASE.$_SESSION['lang'].'/tasks');
						
					
				}
		}
		
		
		$sql = "SELECT
				n.note_id,
				n.title,
				n.description,
				n.priority,
				n.status,
				n.by_user,
				n.start_date,
				n.end_date
			FROM
				notes AS n,
				law_users AS l
			WHERE
			
			l.law_id = '$user_id'
			AND
			l.use_id = n.by_user
		";
		
		
		if($_SESSION['client'] )
		{
					
			$sql .= " AND n.by_user = '".$_SESSION['client']."' ";		
		}
		
}


if (!isset($_GET['sort'])) $_GET['sort'] = 1;
if (!isset($_GET['sortBy'])) $_GET['sortBy'] = 7;

$paging = new Paging($sql, $_GET['sortBy'], $_GET['sort'], array(7=>"Подредба"), 30);
$result = $paging->query();

	
	head('Tasks');
 	if($_SESSION['ACCESS_SID'] == 1) menu_user();
	if($_SESSION['ACCESS_SID'] == 2) menu_law();

?>

			<?php if ($_SESSION['message']) { ?>
            <div class="message green">
                <?=$_SESSION['message']?>
            </div>
            <?php unset($_SESSION['message']); } ?>
            
            <?php if ($error_message) { ?>
            <div class="message red">
                <?=$error_message?>
            </div>
            <?php } ?>


<?php if($_SESSION['ACCESS_SID'] == 2)
         { 
?>
<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>js/calendarDateInput.js"></script>
 
           	
        <div id="newtask">
            <form action="<?=HTML_BASE.$_SESSION['lang']?>/tasks" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_note_sub" value="1" />
            <input type="hidden" name="user_id" value="<?=$u_id?>" />
           
      
            <h4>Нова задача за
            
                
            <?php if($_SESSION['ACCESS_SID'] == 2)
                {
                    if($_SESSION['client'])
                    {
                        echo get_user_name($_SESSION['client']);
                    }else{
            ?>
            
                    (избери клиент)
            <?php
                        
                    }
                }
            ?>
            
            </h4>
             
             <label for="title" class="required">Тема/Заглавие</label>
              
            <input class="inputbox" name="title" type="text" >
            <label>
			<input type="checkbox" name="priority" value="1"> Отбележи като спешно</label>
            
            <br>
            <label>Краен срок</label>
			<div class="date"><script>DateInput('creation_datetime',true,'YYYY-MM-DD',
			'<?=(($_POST['creation_datetime'])?date('Y-m-d',strtotime($_POST['creation_datetime'])):date('Y-m-d'))?>')</script>
            </div>
              
              
            
            <label>Коментар</label>
            <textarea   class="inputbox" style="width:98%" name="description" rows="4"><?=sth($_POST["description"])?></textarea>
            <input class="button" type="submit" name="submit" id="submit" value="запиши">			
			</form> 
         </div>
         
<?php }?>


<div id="content">
		
			
			  <h3>Всички задачи
              
		    <?php if($_SESSION['ACCESS_SID'] == 2)
                {
                    if($_SESSION['client'] )
                    {
                        echo '- на '.get_user_name($_SESSION['client']);
                        
                    }
                }
            ?>
               (<?=$paging->count() ?>) </h3>
           
			  <table id="listing" width="99%" cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                    
                        <td>Задача</td>
                        
                        <td>Краен срок</td>
                        
                        <?php if($_SESSION['ACCESS_SID'] == 2){?>
                        <td>Клиент</td>
                        
                        <td>Действия</td>
                        <?php }?>
                        
                    </tr>
                </thead>
                <tbody>
            <?php
		  $i=1;
			  
			  while ($row = mysql_fetch_object($result)) {				  
			?>
            	
                
                <tr class="<?=(($i%2)?"none":"zebra")?>" id="row-<?=$row->note_id?>">
					
					<td style="border-left:4px solid <?=($row->priority == 1)?'#F30"':'#666'?> ">					
					
						<b><?=sth($row->title)?></b>
                    	<small><?=$row->description?></small>
					</td>
					 
					 
					
					
					<td class="switch">
						<?=date('d M Y', strtotime($row->end_date))?>	
					</td>
					
                    <?php if($_SESSION['ACCESS_SID'] == 2){?>
                             <td style="vertical-align:middle"><?=get_user_name($row->by_user)?></td>
                             
                             <td class="switch" >
								<a onClick="if (window.confirm('Сигурен ли си, че искаш да изтриеш задачата?')) { document.location='<?=HTML_BASE.$_SESSION['lang']?>/tasks?delete=<?=$row->note_id?>'; return false; }" class="delete" title="Изтрий">Изтрий</a>
                              </td>
                     <?php }?>
					
				</tr>
                
                
                
          
         <?php
              $i++;   
            } 
         ?>	    
		 </tbody>
		</table>
			  
			
         	     
        
        <div class="pagingwrap"><div class="paging">
			<?=$paging->display()?>
		</div></div>
			
   </div>
    

<?php
foot();
?>